import { i18n } from '../i18n';

describe('I18nService', () => {
    beforeEach(() => {
        // 重置DOM状态
        document.body.removeAttribute('data-locale');
        // 等待一小段时间让服务重新初始化
        return new Promise(resolve => setTimeout(resolve, 100));
    });

    afterEach(() => {
        // 清理
        document.body.removeAttribute('data-locale');
    });

    describe('tSync method', () => {
        it('should return English translations by default', () => {
            expect(i18n.tSync('navigation.dashboard')).toBe('Dashboard');
            expect(i18n.tSync('navigation.agents')).toBe('Agents');
            expect(i18n.tSync('navigation.materials')).toBe('Materials');
            expect(i18n.tSync('navigation.admin')).toBe('Admin');
            expect(i18n.tSync('navigation.analytics')).toBe('Analytics');
        });

        it('should return Chinese translations when locale is set to zh-CN', () => {
            document.body.setAttribute('data-locale', 'zh-CN');
            // 等待服务重新初始化
            return new Promise(resolve => setTimeout(resolve, 100)).then(() => {
                expect(i18n.tSync('navigation.dashboard')).toBe('首页');
                expect(i18n.tSync('navigation.agents')).toBe('代理');
                expect(i18n.tSync('navigation.materials')).toBe('材料');
                expect(i18n.tSync('navigation.admin')).toBe('管理员');
                expect(i18n.tSync('navigation.analytics')).toBe('分析');
            });
        });

        it('should return admin menu translations in English', () => {
            expect(i18n.tSync('admin.pipelines')).toBe('Pipelines');
            expect(i18n.tSync('admin.environments')).toBe('Environments');
            expect(i18n.tSync('admin.templates')).toBe('Templates');
            expect(i18n.tSync('admin.config_xml')).toBe('Config XML');
            expect(i18n.tSync('admin.package_repositories')).toBe('Package Repositories');
        });

        it('should return admin menu translations in Chinese when locale is set to zh-CN', () => {
            document.body.setAttribute('data-locale', 'zh-CN');
            // 等待服务重新初始化
            return new Promise(resolve => setTimeout(resolve, 100)).then(() => {
                expect(i18n.tSync('admin.pipelines')).toBe('流水线');
                expect(i18n.tSync('admin.environments')).toBe('环境');
                expect(i18n.tSync('admin.templates')).toBe('模板');
                expect(i18n.tSync('admin.config_xml')).toBe('配置XML');
                expect(i18n.tSync('admin.package_repositories')).toBe('包仓库');
            });
        });

        it('should return admin pipelines translations in English', () => {
            expect(i18n.tSync('admin.pipelines.title')).toBe('Pipelines');
            expect(i18n.tSync('admin.pipelines.create.group')).toBe('Create new pipeline group');
            expect(i18n.tSync('admin.pipelines.add.pipeline')).toBe('Add new pipeline');
            expect(i18n.tSync('admin.pipelines.edit.group')).toBe('Edit pipeline group');
            expect(i18n.tSync('admin.pipelines.delete.group')).toBe('Delete pipeline group');
        });

        it('should return admin pipelines translations in Chinese when locale is set to zh-CN', () => {
            document.body.setAttribute('data-locale', 'zh-CN');
            // 等待服务重新初始化
            return new Promise(resolve => setTimeout(resolve, 100)).then(() => {
                expect(i18n.tSync('admin.pipelines.title')).toBe('流水线');
                expect(i18n.tSync('admin.pipelines.create.group')).toBe('创建新的流水线组');
                expect(i18n.tSync('admin.pipelines.add.pipeline')).toBe('添加新流水线');
                expect(i18n.tSync('admin.pipelines.edit.group')).toBe('编辑流水线组');
                expect(i18n.tSync('admin.pipelines.delete.group')).toBe('删除流水线组');
            });
        });

        it('should return key as fallback when translation is not found', () => {
            expect(i18n.tSync('nonexistent.key')).toBe('nonexistent.key');
        });
    });

    describe('t method (async)', () => {
        it('should return English translations by default', async () => {
            const dashboard = await i18n.t('navigation.dashboard');
            const agents = await i18n.t('navigation.agents');
            expect(dashboard).toBe('Dashboard');
            expect(agents).toBe('Agents');
        });

        it('should return Chinese translations when locale is set to zh-CN', async () => {
            document.body.setAttribute('data-locale', 'zh-CN');
            // 等待服务重新初始化
            await new Promise(resolve => setTimeout(resolve, 100));

            const dashboard = await i18n.t('navigation.dashboard');
            const agents = await i18n.t('navigation.agents');
            expect(dashboard).toBe('首页');
            expect(agents).toBe('代理');
        });
    });
});
